package com.lanhao.aot.excel;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.FileUtils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;

public class TxtUtil2 {
	public static final String FILE_ALL = "D:/摄像头数据/合并版.txt";
	
	
	public static void main(String[] args) {
		// 读取集合
		List<String> fileAll = FileUtil.readLines(new File(FILE_ALL), "UTF-8");
		
		Set<String> fileAllSet = new HashSet<String>(fileAll);
		System.out.println("全部数据size=" + fileAllSet.size());
		
		// 从陕西省发货清单汇总.xlsx挑选出存在合并版文档中的摄像头，将数据提取出来
		ExcelReader reader = ExcelUtil.getReader("D:/摄像头数据/陕西省发货清单汇总.xlsx");
		List<List<Object>> readAll = reader.read();
		List<CameraData> sxProcessCameraList = new ArrayList<CameraData>();
		for (int i = 2; i < readAll.size(); i++) {
			List<Object> list = readAll.get(i);
			String sn = (String) list.get(3);
			String cmei = (String) list.get(4);
			String mac = (String) list.get(5);
			if(fileAllSet.contains(cmei)) {
				CameraData cameraData = new CameraData();
				cameraData.setSn(sn);
				cameraData.setCmei(cmei);
				cameraData.setMac(mac);
				sxProcessCameraList.add(cameraData);
			}
		}
		
		System.out.println("陕西省发货清单汇总文件处理完成，在总表中的记录数" + sxProcessCameraList.size());
		
		Map<String, CameraData> cameraDataMap = new HashMap<>();
		ExcelReader reader2 = ExcelUtil.getReader("D:/摄像头数据/XFD400-M全国20220530.xlsx");
//		
		List<List<Object>> readAllCountry = reader2.read();
//		
		for (int i = 1; i < readAllCountry.size(); i++) {
			List<Object> list = readAllCountry.get(i);
			String sn = (String) list.get(0);
			String version = (String) list.get(1);
			String model = (String) list.get(2);
			String online = (String) list.get(3);
			
			CameraData cameraData = new CameraData();
			cameraData.setSn(String.valueOf(sn));
			cameraData.setVersion(version);
			cameraData.setModel(model);
			cameraData.setOnline(online);
			cameraDataMap.put(String.valueOf(sn), cameraData);
		}
		
		System.out.println("处理全国数据记录数" + cameraDataMap.size());
		
		
		for (CameraData data : sxProcessCameraList) {
			CameraData ifExistsData = cameraDataMap.get(data.getSn());
			if(ifExistsData != null) {
				data.setVersion(ifExistsData.getVersion());
				data.setModel(ifExistsData.getModel());
				data.setOnline(ifExistsData.getOnline());
			}
		}
		
		// 生成excel
		
		ExcelWriter writer = ExcelUtil.getWriter("D:/摄像头数据/FinalData.xlsx");
		writer.write(sxProcessCameraList, true);
		//关闭writer，释放内存
		writer.close();
		
	}
}
